<?php
defined('IN_IA') or exit('Access Denied');
global $_W, $_GPC;
$setting = $this->baseset($_W['uniacid']);
load()->func('communication');
$page = $_GPC['page1'];
$pageend = $_GPC['pageend1'];
$itemendprice_min = $_GPC['itemendprice_min'];
$itemendprice_max = $_GPC['itemendprice_max'];
$itemsale_min = $_GPC['itemsale_min'];
$itemsale_max = $_GPC['itemsale_max'];
$tkmoney_min = $_GPC['tkmoney_min'];
$tkmoney_max = $_GPC['tkmoney_max'];
$starttime = $_GPC['starttime'];
$nav = $_GPC['jqnav'];
$cid = $_GPC['jqcid'];
$sort = $_GPC['jqsort'];
$nine = $_GPC['jqnine'];
$thirty = $_GPC['jqthirty'];
$new_today = $_GPC['jqnew_today'];
$tianmao = $_GPC['jqtianmao'];
if (empty($_GPC['no'])) {
	$_GPC['no'] = $page;
	$thispage = $page;
} else {
	$thispage = $_GPC['no'];
}
$newpage = $_GPC['no'] + 1;
$dtkurl = "http://www.haodanku.com/?json=true&cid=" . $cid . "&nav=" . $nav . "&sort=" . $sort . "&starttime=" . $starttime . "&tkmoney_min=" . $tkmoney_min . "&tkmoney_max=" . $tkmoney_max . "&itemsale_min=" . $itemsale_min . "&itemsale_max=" . $itemsale_max . "&itemendprice_min=" . $itemendprice_min . "&itemendprice_max=" . $itemendprice_max . "&nine=" . $nine . "&thirty=" . $thirty . "&new_today=" . $new_today . "&tianmao=" . $tianmao . "&p=" . $thispage;
$resp = ihttp_get($dtkurl);
if (is_error($resp)) {
	$this->json_exit(0, $resp["message"]);
}
$dtkData = json_decode($resp['content'], true);
if ($dtkData) {
	$result = $dtkData;
	$total_i = 0;
	foreach ($result as $key => $item) {
		if ($item) {
			if ($item['itemid'] == '') {
				break;
			}
			if ($item['itemid'] != '' && !empty($item['couponurl'])) {
				$cate = pdo_fetch('select id,itemid from ' . tablename('bsht_tbk_all_caiji') . " where uniacid = '{$uniacid}' AND itemid = '{$item['itemid']}' and uniacid = '{$_W['uniacid']}' limit 1");
				$activity_id = $this->gettkUrl($item['couponurl']);
				$activityid = $activity_id['activity_id'];
				if (empty($activityid)) {
					$activityid = $activity_id['activityId'];
				}
				$newurl = "http://uland.taobao.com/coupon/edetail?activityId=" . $activityid . "&pid=" . $setting['tb_pid'] . "&itemId=" . $item['itemid'] . "&src=pgy_pgyqf&dx=1";
				$tktype = 0;
				if ($item['tktype'] == "通用计划") {
					$tktype = 0;
				}
				if ($item['tktype'] == "定向计划") {
					$tktype = 1;
				}
				if ($item['tktype'] == "鹊桥活动") {
					$tktype = 2;
				}
				$data = array('atime' => TIMESTAMP, 'uniacid' => $_W['uniacid'], 'displayorder' => '0', 'itemid' => $item['itemid'], 'title' => $item['itemtitle'], 'd_title' => $item['itemshorttitle'], 'itempic' => $item['itempic'], 'cate_id' => $item['fqcat'], 'shoptitle' => $item['itemdesc'], 'itemfee' => $item['itemprice'], 'itemfee2' => $item['itemendprice'], 'itemmsell' => $item['itemsale'], 'istmall' => $item['itempic_type'], 'itemyhj_fee' => $item['couponmoney'], 'itemyhj_zl' => $item['couponsurplus'], 'itemyhj_yl' => $item['couponsurplus'] - $item['couponreceive'], 'itemyhj_re' => $item['couponreceive'], 'link_jihua' => $item['tkurl'], 'com_jihua' => '', 'com_queqiao' => '', 'status' => 1, 'wenan' => $item['itemdesc'], 'itemyhj_tit' => $item['couponexplain'], 'itemyhj_url' => $newurl, 'itemurl' => $newurl, 'itemsurl' => $newurl, 'quan_link' => $item['couponurl'], 'shouru_bili' => $item['tkrates'], 'shouru_yongjin' => $item['tkmoney'], 'quan_id' => $activityid, 'maijia_id' => $item['userid'], 'tktype' => $tktype, 'fm' => 'dtk',);
				$data['itemyhj_tkl'] = '';
				$data['itemtkl'] = '';
				$data['itemyhj_stime'] = date("Y-m-d H:i:s", $item['couponstarttime']);
				$data['itemyhj_etime'] = date("Y-m-d H:i:s", $item['couponendtime']);
				if (empty($cate)) {
					pdo_insert('bsht_tbk_all_caiji', $data);
					$cateid = pdo_insertid();
				} else {
					pdo_update($this->modulename . '_all_caiji', $data, array('id' => $cate['id']));
					$cateid = $cate['id'];
				}
			}
			$total_i++;
		} else {
		}
	}
	if ($thispage == $pageend) {
		message('采集成功！', $this->createWebUrl('listcaijiall_hdk'), 'success');
	} else {
		message('即将导入第' . $newpage . '页（每页120条），请稍后。。。', $this->createWebUrl('importall_hdk1', array('page1' => $page, 'pageend1' => $pageend, 'no' => $newpage, 'itemendprice_min' => $itemendprice_min, 'itemendprice_max' => $itemendprice_max, 'itemsale_min' => $itemsale_min, 'itemsale_max' => $itemsale_max, 'tkmoney_min' => $tkmoney_min, 'tkmoney_max' => $tkmoney_max, 'starttime' => $starttime, 'jqnav' => $nav, 'jqcid' => $cid, 'jqsort' => $sort, 'jqnine' => $nine, 'jqthirty' => $thirty, 'jqnew_today' => $new_today, 'jqtianmao' => $tianmao)), 'success');
	}
} else {
}
message('采集成功！', $this->createWebUrl('listcaijiall_hdk'), 'success');